<?php  /* FORUMS $Id: view_messages.php,v 1.37.4.3 2006/03/19 19:37:59 pedroix Exp $ */
$AppUI->savePlace();
$sort = dPgetParam($_REQUEST, 'sort', 'asc');
$viewtype = dPgetParam($_REQUEST, 'viewtype', 'normal');
$hideEmail = dPgetConfig('hide_email_addresses', false );

$q  = new DBQuery;
$q->addTable('forums');
$q->addTable('forum_messages');
$q->addQuery('forum_messages.*,	contact_first_name, contact_last_name, contact_email, user_username,
		forum_moderated, visit_user');
$q->addJoin('forum_visits', 'v', "visit_user = {$AppUI->user_id} AND visit_forum = $forum_id AND visit_message = 				forum_messages.message_id");
$q->addJoin('users', 'u', 'message_author = u.user_id');
$q->addJoin('contacts', 'con', 'contact_id = user_contact');
$q->addWhere("forum_id = message_forum AND (message_id = $message_id OR message_parent = $message_id)");
$q->addOrder("message_date $sort"); 

$messages = $q->loadList();

$crumbs = array();
$crumbs['?m=forums&a=viewer&forum_id='.$forum_id] = "topics for this forum";
//$crumbs['?m=forums&a=view_pdf&forum_id=$forum_id&message_id=$message_id&sort=$sort&suppressHeaders=1'] = "view PDF file";
?>
<script language="javascript">
<?php
if ($viewtype != 'normal')
{
?>
        function toggle(id)
        {
<?php
if ($viewtype == 'single')
{
?>
                var elems = document.getElementsByTagName("div");
                for (var i=0; i<elems.length; i++)
                  if (elems[i].className == 'message')
                        elems[i].style.display = 'none';
                document.getElementById(id).style.display = 'block';

<?php 
}
else if ($viewtype=='short')
{
?>
                vista = (document.getElementById(id).style.display == 'none') ? 'block' : 'none';
                document.getElementById(id).style.display = vista;
<?php
}
?>
        }
<?php 
}
if ($canEdit) {
?>
function doDeletePost(id){
	var form = document.messageForm;
	if (confirm( "<?=$AppUI->_('forumsDelete')?>" )) {
		form.del.value = 1;
		form.message_id.value = id;
		form.submit();
	}
}

function doPostMessage() {
	var form = document.msgforum;
	if (form.message_title.value.search(/^\s*$/) >= 0 ) {
		alert("<?=$AppUI->_('forumSubject', UI_OUTPUT_JS)?>");
		form.message_title.focus();
	} else if (form.message_body.value.search(/^\s*$/) >= 0) {
		alert("<?=$AppUI->_('forumTypeMessage', UI_OUTPUT_JS)?>");
		form.message_body.focus();
	} else {
		form.submit();
	}
}

var posturl = 'index.php?m=forums&a=post&forum_id=<?=$forum_id?>&post_message=1&suppressHeaders=1';

function doCreateTopic() {
	ajax.send(posturl,'',
		function (r) {
			$('idPostMessage').innerHTML = r;
			Dialog('idPostMessage').show();
		}
	);
}

function doReplyTopic() {
	ajax.send(posturl+'&message_parent=<?=$message_id?>','',
		function (r) {
			$('idPostMessage').innerHTML = r;
			Dialog('idPostMessage').show();
		}
	);
}

function doEditPost(pid, id) {
	ajax.send(posturl+'&message_parent='+pid+'&message_id='+id,'',
		function (r) {
			$('idPostMessage').innerHTML = r;
			Dialog('idPostMessage').show();
		}
	);
}

function doCloseDialog() {
	Dialog('idPostMessage').hide();
}
<?php } ?>
</script>
<?php
$thispage = "?m=$m&a=viewer&forum_id=$forum_id&message_id=$message_id&sort=$sort";
// $thispage = $_PHP['self'];
?>
<div id="idPostMessage" style="visibility: hide" class="popup">
</div>
<table width="100%" cellspacing="0" cellpadding="8" border="0" class="subinfopanel">
<tr>
	<td style="padding-right: 0px">
		<form action="<?=$thispage?>" method="post">
		<?=$AppUI->_('View') ?>
	</td>
	<td width="100%" style="padding-left: 0px">
		<input type="radio" name="viewtype" value="normal" <?=($viewtype == 'normal')?'checked':''?>
			onClick="this.form.submit();" /><?=$AppUI->_('Normal') ?>
		<input type="radio" name="viewtype" value="short" <?=($viewtype == 'short')?'checked':''?>
			onClick="this.form.submit();" /><?=$AppUI->_('Collapsed') ?>
		<input type="radio" name="viewtype" value="single" <?=($viewtype == 'single')?'checked':''?>
			onClick="this.form.submit();" /><?=$AppUI->_('Single Message at a time') ?>
		</form>
	</td>
	<td align="right" nowrap="nowrap">
		<?php $sort = ($sort == 'asc')?'desc':'asc'?>
		<input type="button" class=button value="<?=$AppUI->_('Sort By Date').' ('.$AppUI->_($sort).')'?>"
			onclick="go('index.php?m=forums&a=viewer&forum_id=<?=$forum_id?>&message_id=<?=$message_id?>&sort=<?=$sort?>')"/>
		<?php if ($canEdit) { ?>
		<input type="button" class=button value="<?=$AppUI->_('Post Reply')?>" onclick="doReplyTopic()"/>
		<input type="button" class=button value="<?=$AppUI->_('New Topic')?>" onclick="doCreateTopic()"/>
	<?php } ?>
	</td>
</tr>
</table>

<table border="0" cellpadding="4" cellspacing="0" width="100%" class="list">
<form name="messageForm" method="POST" action="?m=forums&forum_id=<?=$forum_id?>">
	<input type="hidden" name="dosql" value="do_post_aed" />
	<input type="hidden" name="del" value="0" />
	<input type="hidden" name="message_id" value="0" />
</form>
<tr>
<?php 
if ($viewtype != 'short') echo '<th nowrap>' .$AppUI->_('Author') .':</th>';
echo '<th class="last" width="100%">'. $AppUI->_('Message').':</th>';
?>
</tr>

<?php 
$x = false;

$date = new CDate();

if ($viewtype == 'single')
{
	$s = '';
        $first = true;
}

$new_messages = array();

foreach ($messages as $row) {
	// Find the parent message - the topic.
	if ($row['message_id'] == $message_id)
		$topic = $row['message_title'];
		
	$q  = new DBQuery;
	$q->addTable('forum_messages');
	$q->addTable('users');
	$q->addQuery('DISTINCT contact_email, contact_first_name, contact_last_name, user_username');
	$q->addJoin('contacts', 'con', 'contact_id = user_contact');
	$q->addWhere('users.user_id = '.$row['message_editor']);
	$editor = $q->loadList();

	$date = intval( $row['message_date'] ) ? new CDate( $row['message_date'] ) : null;
	if ($viewtype != 'single') $s = '';
	//$style = $x ? 'background-color:#eeeeee' : '';
	$style="";

//!!! Different table building for the three different views
// To be cleaned up, and reuse common code at later stage.
if ($viewtype =='normal')
{
	$s .= '<tr class="second">';

	$s .= '<td valign="top" nowrap="nowrap">';
	if ( ! $hideEmail) {
			$s .= '<a href="mailto:'.$row['contact_email'].'">';
	}
	$s .= $row['contact_first_name'].' '.$row['contact_last_name'];
	if ( ! $hideEmail) {
		$s .= '</a>';
	}
	if (sizeof($editor)>0) {
		$s .= '<br/>&nbsp;<br/>'.$AppUI->_('last edited by');
		$s .= ':<br/>';
		if ( !$hideEmail) {
			$s .= '<a href="mailto:'.$editor[0]['contact_email'].'">';
		}
		$s .= '<font size="1">'.$editor[0]['contact_first_name'].' '.$editor[0]['contact_last_name'].'</font>';
		if (! $hideEmail) {
			$s .= '</a>';
		}
	}
	if ($row['visit_user'] != $AppUI->user_id) {
		$s .= "<br/>&nbsp;" . dPshowImage('images/icons/stock_new_small.png');
		$new_messages[] = $row['message_id'];
	}
	$s .= '</td>';
	$s .= '<td valign="top" style="'.$style.'">';
	$s .= '<strong>'.$row['message_title'].'</strong><hr size="1" style="padding: 0px">';
	$s .= str_replace( chr(13), "&nbsp;<br />", $row['message_body'] );
	$s .= '</td>';

	$s .= '</tr><tr>';

	$s .= '<td nowrap="nowrap">';
	$s .= '<img src="images/icons/posticon.gif">'.$date->format( "$df $tf" ).'</td>';
	$s .= '<td align="right">';

	//the following users are allowed to edit/delete a forum message: 1. the forum creator  2. a superuser with read-write access to 'all' 3. the message author
	$canEdit = $perms->checkModuleItem('forums', 'edit', $row['message_id']);
	if ( $canEdit && ( $AppUI->user_id == $row['forum_moderated'] || $AppUI->user_id == $row['message_author'] || $perms->checkModule('admin', 'edit'))) {
		$s .= '<table cellspacing="0" cellpadding="0" border="0" class="internal"><tr>';
	// edit message
//		$s .= '<td><a href="index.php?m=forums&a=viewer&post_message=1&forum_id='.$row['message_forum'].'&message_parent='.$row['message_parent'].'&message_id='.$row['message_id'].'" title="'.$AppUI->_( 'Edit' ).'">';
		$s .= '<td><a href="javascript:doEditPost('.$row['message_parent'].','.$row['message_id'].')" title="'.$AppUI->_( 'Edit' ).'">';
		$s .= dPshowImage( 'images/edit.gif' );
		$s .= '</td><td>';
	// delete message
		$s .= '<a href="javascript:doDeletePost('.$row['message_id'].')" title="'.$AppUI->_( 'Delete' ).'">';
		$s .= dPshowImage( 'images/common_delete.gif' );
		$s .= '</a>';
		$s .= '</td></tr></table>';

	}
	$s .= '</td>';

	$s .= '</tr>';
}
else if ($viewtype == 'short')
{
        $s .= '<tr class="second">';

        $s .= '<td valign="top" style="'.$style.'" >';
        $s .= '<a href="mailto:'.$row['contact_email'].'">';
        $s .= '<font size="2">'.$row['contact_first_name'].' '.$row['contact_last_name'].'</font></a>';
        $s .= ' (' . $date->format( "$df $tf" ) . ') ';
        if (sizeof($editor)>0) {
                $s .= '<br/>&nbsp;<br/>'.$AppUI->_('last edited by');
                $s .= ':<br/><a href="mailto:'.$editor[0]['contact_email'].'">';
                $s .= '<font size="1">'.$editor[0]['contact_first_name'].' '.$editor[0]['contact_last_name'].'</font></a>';
        }
  $s .= '<a name="'.$row['message_id'].'" href="#'.$row['message_id'].'" onClick="toggle('.$row['message_id'].')">';
        $s .= '<span size="2"><strong>'.$row['message_title'].'</strong></span></a>';
        $s .= '<div class="message" id="'.$row['message_id'].'" style="display: none">';
        $s .= str_replace( chr(13), "&nbsp;<br />", $row['message_body'] );
        $s .= '</div></td>';

        $s .= '</tr>';
}
else if ($viewtype == 'single')
{
        $s .= '<tr class="second">';

        $s .= '<td valign="top" style="'.$style.'">';
        $s .= $date->format( "$df $tf" ).' - ';
        $s .= '<a href="mailto:'.$row['contact_email'].'">';
        $s .= '<font size="2">'.$row['contact_first_name'].' '.$row['contact_last_name'].'</font></a>';
        $s .= '<br />';
        if (sizeof($editor)>0) {
                $s .= '<br/>&nbsp;<br/>'.$AppUI->_('last edited by');
                $s .= ':<br/><a href="mailto:'.$editor[0]['contact_email'].'">';
                $s .= '<font size="1">'.$editor[0]['contact_first_name'].' '.$editor[0]['contact_last_name'].'</font></a>';
        }
  $s .= '<a href="#" onClick="toggle('.$row['message_id'].')">';
        $s .= '<span size="2"><strong>'.$row['message_title'].'</strong></span></a>';
        $side .= '<div class="message" id="'.$row['message_id'].'" style="display: none">';
        $side .= str_replace( chr(13), "&nbsp;<br />", $row['message_body'] );
        $side .= '</div>';
        $s .= '</td>';
        if ($first)
        {
                $s .= '<td rowspan="'.count($messages).'" valign="top">';
                echo $s;
                $s = '';
                $first = false;
        }

        $s .= '</tr>';
}

if ($viewtype != 'single')
	echo $s;
	$x = !$x;

}
if ($viewtype == 'single')
        echo $side . '</td>' . $s;
?>
</table>
<table border=0 cellpadding=2 cellspacing=1 width="98%" >
<tr>
	<td><?=breadCrumbs( $crumbs )?></td>
	<td align="right">
		<input type="button" class=button value="<?=$AppUI->_('Sort By Date') . ' (' . $AppUI->_($sort) . ')'?>" onClick="go('index.php?m=forums&a=viewer&forum_id=<?=$forum_id?>&message_id=<?=$message_id?>&sort=<?=$sort?>')" />
	<?php if ($canEdit) { ?>
		<input type="button" class="button" value="<?=$AppUI->_('Post Reply')?>" onclick="go('index.php?m=forums&a=viewer&forum_id=<?=$forum_id?>&message_parent=<?=$message_id?>&post_message=1')" />
		<input type="button" class="button" value="<?=$AppUI->_('New Topic')?>" onclick="go('index.php?m=forums&a=viewer&forum_id=<?=$forum_id?>&message_id=0&post_message=1')" />
	<?php } ?>
	</td>
</tr>
</table>
<?php
  // Now we need to update the forum visits with the new messages so they don't show again.
  foreach ($new_messages as $msg_id) {
	$q  = new DBQuery;
	$q->addTable('forum_visits');
	$q->addInsert('visit_user', $AppUI->user_id);
	$q->addInsert('visit_forum', $forum_id);
	$q->addInsert('visit_message', $msg_id);
	$q->addInsert('visit_date', $date->getDate());
	$q->exec();
	$q->clear();
  }
?>
